package com.niiwoo.civet.trade.listener;

import java.io.IOException;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.niiwoo.civet.trade.constant.RabbitConstant;
import com.niiwoo.civet.trade.service.local.intelligenceInvest.IntelligenceInvestService;
import com.rabbitmq.client.Channel;

import lombok.extern.slf4j.Slf4j;

/**
 * 智能投合同处理MQ
 * @author liuchun
 *
 * 2018年11月16日
 */
@Slf4j
@Component
public class IntelligenceContractEventListener {
	@Autowired
	private IntelligenceInvestService intelligenceInvestService;
	
	@RabbitListener(queues = {RabbitConstant.Queue.INTELLIGENCE_INVEST_CONTRACT})
	public void investEventSend(Long projectPackageInvestOrderId, Message message, Channel channel) throws IOException {
		log.info("智能投接收合同MQ发{}", projectPackageInvestOrderId);
		Boolean isAck = true;
		try {
			intelligenceInvestService.initIntelligenceContract(projectPackageInvestOrderId);
		}  catch (Exception e) {
			 
			isAck = false;
			try {
				Thread.sleep(5 * 1000L);
			} catch (Exception expect) {
			}
		} finally {
			if (isAck) {
				channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
			} else {
				channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
			}
		}
	}
}
